home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / toolbox / gethelpstrings / dsuserprocs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-20  |  15.1 KB  |  510 lines

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSUserProcs.c
  5. **
  6. **   Description:    Specific AppleEvent handlers used by the DropBox
  7. **
  8. *******************************************************************************
  9. **                       A U T H O R   I D E N T I T Y
  10. *******************************************************************************
  11. **
  12. **    Initials    Name
  13. **    --------    -----------------------------------------------
  14. **    LDR            Leonard Rosenthol
  15. **    MTC            Marshall Clow
  16. **    SCS            Stephan Somogyi
  17. **
  18. *******************************************************************************
  19. **                      R E V I S I O N   H I S T O R Y
  20. *******************************************************************************
  21. **
  22. **      Date        Time    Author    Description
  23. **    --------    -----    ------    ---------------------------------------------
  24. **    06/23/94            LDR        Added support for ProcessItem and ProcessFolder handling
  25. **    02/20/94            LDR        Modified Preflight & Postflight to take item count
  26. **    01/25/92            LDR        Removed the use of const on the userDataHandle
  27. **    12/09/91            LDR        Added the new SelectFile userProc
  28. **                                Added the new Install & DisposeUserGlobals procs
  29. **                                Modified PostFlight to only autoquit on odoc, not pdoc
  30. **    11/24/91            LDR        Added the userProcs for pdoc handler
  31. **                                Cleaned up the placement of braces
  32. **                                Added the passing of a userDataHandle
  33. **    10/29/91            SCS        Changes for THINK C 5
  34. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  35. **                                Added a bunch of comments for clarification
  36. **    10/06/91    00:02    MTC        Converted to MPW C
  37. **    04/09/91    00:02    LDR        Added to Projector
  38. **
  39. ******************************************************************************/
  40.  
  41. #include <StandardFile.h>
  42. #include <Resources.h>
  43. #include <Balloons.h>
  44.  
  45. #include "DSGlobals.h"
  46. #include "DSUserProcs.h"
  47.  
  48. #include "acur.h"
  49.  
  50. // Static Prototypes
  51. static OSErr ProcessItem(FSSpecPtr myFSSPtr);
  52. static OSErr ProcessFolder(FSSpecPtr myFSSPtr);
  53.  
  54.  
  55. /*
  56.     Uncomment this line if you want each item of a dropped folder processed
  57.     as an individual item
  58. */
  59. #define qWalkFolders
  60.  
  61.  
  62. /*
  63.     This routine is called during init time.
  64.     
  65.     It allows you to install more AEVT Handlers beyond the standard four
  66. */
  67. #pragma segment Main
  68. pascal void InstallOtherEvents (void) {
  69. }
  70.  
  71.  
  72. /*    
  73.     This routine is called when an OAPP event is received.
  74.     
  75.     Currently, all it does is set the gOApped flag, so you know that
  76.     you were called initally with no docs, and therefore you shouldn't 
  77.     quit when done processing any following odocs.
  78. */
  79. #pragma segment Main
  80. pascal void OpenApp (void) {
  81.     gOApped = true;
  82. }
  83.  
  84.  
  85. /*    
  86.     This routine is called when an QUIT event is received.
  87.     
  88.     We simply set the global done flag so that the main event loop can
  89.     gracefully exit.  We DO NOT call ExitToShell for two reasons:
  90.     1) It is a pretty ugly thing to do, but more importantly
  91.     2) The Apple event manager will get REAL upset!
  92. */
  93. #pragma segment Main
  94. pascal void QuitApp (void) {
  95.     gDone = true;    /*    All Done! */
  96. }
  97.  
  98.  
  99. /*    
  100.     This routine is the first one called when an ODOC or PDOC event is received.
  101.     
  102.     In this routine you would place code used to setup structures, etc. 
  103.     which would be used in a 'for all docs' situation (like "Archive all
  104.     dropped files")
  105.  
  106.     Obviously, the opening boolean tells you whether you should be opening
  107.     or printing these files based on the type of event recieved.
  108.     
  109.     NEW IN 2.0!
  110.     The itemCount parameter is simply the number of items that were dropped on
  111.     the application and that you will be processing.  This gives you the ability
  112.     to do a single preflight for memory allocation needs, rather than doing it
  113.     once for each item as in previous versions.
  114.     
  115.     userDataHandle is a handle that you can create & use to store your own
  116.     data structs.  This dataHandle will be passed around to the other 
  117.     odoc/pdoc routines so that you can get at your data without using
  118.     globals - just like the new StandardFile.  
  119.     
  120.     We also return a boolean to tell the caller if you support this type
  121.     of event.  By default, our dropboxes don't support the pdoc, so when
  122.     opening is FALSE, we return FALSE to let the caller send back the
  123.     proper error code to the AEManager.
  124.  
  125.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  126. */
  127. #pragma segment Main
  128. pascal Boolean PreFlightDocs (Boolean opening, short itemCount, Handle *userDataHandle) {
  129. #pragma unused ( itemCount )
  130. #pragma unused ( userDataHandle )
  131.  
  132.     return opening;        // we support opening, but not printing - see above
  133. }
  134.  
  135.  
  136. /*    
  137.     This routine is called for each file passed in the ODOC event.
  138.     
  139.     In this routine you would place code for processing each file/folder/disk that
  140.     was dropped on top of you.
  141.     
  142.     You will probably want to remove the #pragma unused (currently there to fool the compiler!)
  143. */
  144. #pragma segment Main
  145. pascal void OpenDoc ( FSSpecPtr myFSSPtr, Boolean opening, Handle userDataHandle ) {
  146. #pragma unused ( myFSSPtr )
  147. #pragma unused ( opening )
  148. #pragma unused ( userDataHandle )
  149.     OSErr    err = noErr;
  150.     
  151.     
  152.     #ifdef qWalkFolders
  153.     /*
  154.         For this case we need to determine if the FSSpec is a file or folder.
  155.         If it's a folder, we then need to process each item in that folder,
  156.         otherwise just process the item.
  157.     */
  158.     if (FSpIsFolder(myFSSPtr))
  159.         err = ProcessFolder(myFSSPtr);
  160.     else
  161.         err = ProcessItem(myFSSPtr);
  162.     #else
  163.     /*
  164.         For this case we just call ProcessItem on the FSSpec above.
  165.     */
  166.     err = ProcessItem(myFSSPtr);
  167.     #endif
  168.     
  169.     // you should probably do something if you get back an error ;)
  170. }
  171.  
  172.  
  173. /*    
  174.     This routine is the last routine called as part of an ODOC event.
  175.     
  176.     In this routine you would place code to process any structures, etc. 
  177.     that you setup in the PreflightDocs routine.
  178.  
  179.     NEW IN 2.0!
  180.     The itemCount parameter was the number of items that you processed.
  181.     It is passed here just in case you need it ;)  
  182.     
  183.     If you created a userDataHandle in the PreFlightDocs routines, this is
  184.     the place to dispose of it since the Shell will NOT do it for you!
  185.     
  186.     You will probably want to remove the #pragma unusued (currently there to fool the compiler!)
  187. */
  188. #pragma segment Main
  189. pascal void PostFlightDocs ( Boolean opening, short itemCount, Handle userDataHandle ) {
  190. #pragma unused ( opening )
  191. #pragma unused ( itemCount )
  192. #pragma unused ( userDataHandle )
  193.  
  194.     if ( (opening) && (!gOApped) )
  195.         gDone = true;    //    close everything up!
  196.  
  197.     /*
  198.         The reason we do not auto quit is based on a recommendation in the
  199.         Apple event Registry which specifically states that you should NOT
  200.         quit on a 'pdoc' as the Finder will send you a 'quit' when it is 
  201.         ready for you to do so.
  202.     */
  203. }
  204.  
  205. /*
  206.     This routine gets called for each item (which could be either a file or a folder)
  207.     that the caller wants dropped.  The determining factor is the definition of the 
  208.     qWalkFolder compiler directive.   Either way, the item in question should be
  209.     processed as a single item and not "dissected" into component units (like subfiles
  210.     of a folder!)
  211. */
  212.  
  213. /*
  214.     In our case, we try to extract the help message.  If we succeed, write the name
  215.     of the file, along with the message if possible.  Note that even if we have
  216.     failures in this procedure, we return noErr.  This guarantees that we continue
  217.     to process other files despite any errors that may happen regarding this file.
  218.     
  219.     We only process strings 
  220.         1) in the help message record itself
  221.         2) in a 'STR#' resource
  222.         3) in a 'STR ' resource
  223. */
  224. OSErr ProcessItem(FSSpecPtr myFSSPtr)
  225. {
  226.     OSErr             err;
  227.     long            count;
  228.     short            resRefNum;
  229.     HMMessageRecord    aHelpMsg;
  230.     Str255            theString;
  231.     StringHandle    msgHdl;
  232.     
  233.     SpinCursor(1);
  234.     resRefNum = FSpOpenResFile(myFSSPtr, fsRdPerm);
  235.     err = ResError();
  236.     if (err) goto leaveme;
  237.     
  238.     err = HMExtractHelpMsg( kHMFinderApplResType,
  239.                             kHMHelpID,
  240.                             1,                 // should only be 1 Finder help string
  241.                             kHMEnabledItem, // what to specify for kHMFinderApplResType
  242.                                             // (which doesn't use whichState field really)
  243.                             &aHelpMsg );
  244.     if (err) goto leaveme;
  245.     
  246. // Write out the name of the file being processed
  247. #define kSeparator    "\p\r*****\r"
  248.     count = myFSSPtr->name[0];
  249.     err = FSWrite( gOutputRefNum, &count, &(myFSSPtr->name[1]) );
  250.     BlockMoveData(kSeparator, theString, sizeof(kSeparator));
  251.     count = theString[0];
  252.     err = FSWrite( gOutputRefNum, &count, theString+1 );
  253.     
  254.     switch (aHelpMsg.hmmHelpType)
  255.     {
  256.         case khmmString:
  257.         case -1:            // Resorcerer puts wrong value in hmmHelpType
  258.             count = aHelpMsg.u.hmmString[0];
  259.             err = FSWrite( gOutputRefNum, &count, &(aHelpMsg.u.hmmString[1]) );
  260.             break;
  261.  
  262.         case khmmStringRes:
  263.             GetIndString(theString, 
  264.                         aHelpMsg.u.hmmStringRes.hmmResID, 
  265.                         aHelpMsg.u.hmmStringRes.hmmIndex);
  266.             err = ResError();
  267.             if (err) goto leaveme;
  268.  
  269.             count = theString[0];
  270.             err = FSWrite( gOutputRefNum, &count, theString+1 );
  271.             break;
  272.  
  273.         case khmmTERes:
  274.             msgHdl = (StringHandle)GetResource(kHMTETextResType, aHelpMsg.u.hmmTERes);
  275.             err = ResError();
  276.             if (err) goto leaveme;
  277.  
  278.             HLock((Handle)msgHdl);
  279.             count = GetHandleSize((Handle)msgHdl);
  280.             err = FSWrite( gOutputRefNum, &count, *msgHdl );
  281.             HUnlock((Handle)msgHdl);
  282.             break;
  283.             break;
  284.             
  285.         case khmmSTRRes:
  286.         {
  287.             
  288.             msgHdl = GetString(aHelpMsg.u.hmmSTRRes);
  289.             err = ResError();
  290.             if (err) goto leaveme;
  291.  
  292.             HLock((Handle)msgHdl);
  293.             count = *msgHdl[0];
  294.             err = FSWrite( gOutputRefNum, &count, *msgHdl+1 );
  295.             HUnlock((Handle)msgHdl);
  296.             break;
  297.         }
  298.  
  299.         default:
  300.             #define kCantHandle    "\p<Could not process help resource>"
  301.             BlockMove(kCantHandle, theString, sizeof(kCantHandle) );
  302.             count = theString[0];
  303.             err = FSWrite( gOutputRefNum, &count, theString+1 );
  304.             break;
  305.     }
  306.     
  307.     count = 2;
  308.     theString[0] = '\r';
  309.     theString[1] = '\r';
  310.     err = FSWrite( gOutputRefNum, &count, theString );
  311.  
  312. leaveme:
  313.     CloseResFile(resRefNum);
  314.     return noErr;        // want to continue no matter what errors we encounter.
  315. }
  316.  
  317.  
  318.  
  319. /*
  320.     This routine gets called for any folder (or disk) that the caller wants 
  321.     processed as a set of component items, instead of as a single entity.
  322.     The determining factor is the definition of the qWalkFolder compiler directive.
  323. */
  324. static OSErr ProcessFolder(FSSpecPtr myFSSPtr)
  325. {
  326.     OSErr        err = noErr;
  327.     short        index, oldIndex, localIndex;
  328.     FSSpec        localFSSpec, curFSSpec;
  329.     CInfoPBRec    cipb;
  330.     Str255        fName, vFName;
  331.     long        dirID, origDirID;
  332.     Boolean        foundPosition;
  333.  
  334.      // copy the source locally to avoid recursion problems
  335.      BlockMoveData(myFSSPtr, &localFSSpec, sizeof(FSSpec));
  336.      
  337.     //    get the dirID for THIS folder, not it's parent!
  338.     BlockMoveData(localFSSpec.name, fName, 32);
  339.     
  340.     cipb.hFileInfo.ioCompletion    = 0L;
  341.     cipb.hFileInfo.ioNamePtr    = fName;
  342.     cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  343.     cipb.hFileInfo.ioFDirIndex    = 0;    // use the dir & vRefNum;
  344.     cipb.hFileInfo.ioDirID        = localFSSpec.parID;
  345.     err = PBGetCatInfoSync(&cipb);
  346.     
  347.     if (!err) {        
  348.         origDirID = cipb.dirInfo.ioDrDirID; // copy the sucker
  349.         index = 1;
  350.                 
  351.         // index through all contents of this folder
  352.         while (err == noErr) {
  353.             dirID = origDirID;
  354.             localIndex = index;
  355.             fName [0] = 0;
  356.             cipb.hFileInfo.ioCompletion    = 0L;
  357.             cipb.hFileInfo.ioNamePtr    = fName;
  358.             cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  359.             cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  360.             cipb.hFileInfo.ioDirID        = dirID;
  361.             err = PBGetCatInfoSync(&cipb);
  362.  
  363.             if (!err) {
  364.                 BlockMoveData(fName, curFSSpec.name, 32);
  365.                 curFSSpec.vRefNum    = cipb.hFileInfo.ioVRefNum;
  366.                 curFSSpec.parID        = dirID;
  367.             
  368.                 /*    
  369.                     Check to see if this entry is a folder.
  370.                 */
  371.                 if (cipb.hFileInfo.ioFlAttrib & ioDirMask) {
  372.                     err = ProcessFolder(&curFSSpec);
  373.                     if (err == fnfErr)
  374.                         err = noErr;
  375.                  } else
  376.                     err = ProcessItem(&curFSSpec);
  377.             
  378.                 /*    If we've had an error, get out! */
  379.                 if (err)    break;
  380.  
  381.                 // dirID = origDirID;    
  382.                 localIndex = index;    
  383.  
  384.                 /*    
  385.                     Now take into account new files being created
  386.                     in the current directory & messing up our index.
  387.                     See Dev.CD Vol. XI:Tools & Apps (Moof!):Misc Utilities:
  388.                     Disinfectant & Source 2.5.1:Sample:Notes:Scan Alg    
  389.                 */
  390.                 vFName [0] = 0;
  391.                 cipb.hFileInfo.ioCompletion    = 0L;
  392.                 cipb.hFileInfo.ioNamePtr    = vFName;
  393.                 cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  394.                 cipb.hFileInfo.ioFDirIndex    = localIndex;    // use a real index
  395.                 cipb.hFileInfo.ioDirID        = dirID;
  396.                 err = PBGetCatInfoSync(&cipb);
  397.                 oldIndex = index;
  398.                 if (!err) {
  399.                     /*    If they're equal - same place, go to next */
  400.                     if (EqualString (vFName, fName, false, false))
  401.                         index++;
  402.                 }
  403.                 
  404.                 /*    If we didn't advance, then perhaps a file was created or deleted */
  405.                 if (oldIndex == index) {
  406.                     oldIndex        = index;    /* save off the old */
  407.                     index            = 0;        /* and start at the beginning */
  408.                     err                = noErr;
  409.                     vFName [0]        = 0;
  410.                     foundPosition    = false;
  411.                     
  412.                     while (!foundPosition) {
  413.                         index++;
  414.                         vFName [0] = 0;
  415.                         cipb.hFileInfo.ioCompletion    = 0L;
  416.                         cipb.hFileInfo.ioNamePtr    = vFName;
  417.                         cipb.hFileInfo.ioVRefNum    = localFSSpec.vRefNum;
  418.                         cipb.hFileInfo.ioFDirIndex    = index;    /* now use a real index */
  419.                         cipb.hFileInfo.ioDirID        = dirID;
  420.                         err = PBGetCatInfoSync(&cipb);
  421.                         
  422.                         if (err == fnfErr) {  // we've just been deleted
  423.                             index = oldIndex;
  424.                             foundPosition = true;
  425.                             err = noErr;    // have to remember to reset this!
  426.                         }
  427.                         
  428.                     /*    found same file & same index position */
  429.                     /*    so try the next item */
  430.                         if ((!foundPosition) && EqualString(fName, vFName, false, false)) {
  431.                             index++;
  432.                             foundPosition = true;
  433.                         }
  434.                     }
  435.                 }
  436.             }
  437.         }
  438.     }
  439.     
  440.     return(err);
  441. }
  442.  
  443. /*
  444.     This routine is called when the user chooses "Select File…" from the
  445.     File Menu.
  446.     
  447.     Currently it simply calls the new StandardGetFile routine to have the
  448.     user select a single file (any type, numTypes = -1) and then calls the
  449.     SendODOCToSelf routine in order to process it.  
  450.             
  451.     The reason we send an odoc to ourselves is two fold: 1) it keeps the code
  452.     cleaner as all file openings go through the same process, and 2) if events
  453.     are ever recordable, the right things happen (this is called Factoring!)
  454.  
  455.     Modification of this routine to only select certain types of files, selection
  456.     of multiple files, and/or handling of folder & disk selection is left 
  457.     as an exercise to the reader.
  458. */
  459. pascal void SelectFile (void)
  460. {
  461.     StandardFileReply    stdReply;
  462.     SFTypeList            theTypeList;
  463.  
  464.     StandardGetFile(NULL, -1, theTypeList, &stdReply);
  465.     if (stdReply.sfGood)    // user did not cancel
  466.         SendODOCToSelf(&stdReply.sfFile);    // so send me an event!
  467. }
  468.  
  469. /*
  470.     This routine is called during the program's initialization and gives you
  471.     a chance to allocate or initialize any of your own globals that your
  472.     dropbox needs.
  473.     
  474.     You return a boolean value which determines if you were successful.
  475.     Returning false will cause DropShell to exit immediately.
  476. */
  477. pascal Boolean InitUserGlobals(void)
  478. {
  479.     OSErr    err;
  480.     StandardFileReply reply;
  481.     
  482.     StandardPutFile( "\pEnter name for result file", "\pHelp Strings", &reply );    
  483.     if (!reply.sfGood)
  484.         return(false);
  485.  
  486.     if (reply.sfReplacing)
  487.     {
  488.         FSpDelete(&reply.sfFile);
  489.     }
  490.     err = FSpCreate( &reply.sfFile, 'ttxt' , 'TEXT', smSystemScript );        
  491.     if (err)
  492.         return (false);
  493.     
  494.     err = FSpOpenDF(&reply.sfFile, fsRdWrPerm, &gOutputRefNum);
  495.     if (err) return(false);
  496.     
  497.     InitCursors();
  498.     return(true);    // successful!
  499. }
  500.  
  501. /*
  502.     This routine is called during the program's cleanup and gives you
  503.     a chance to deallocate any of your own globals that you allocated 
  504.     in the above routine.
  505. */
  506. pascal void DisposeUserGlobals(void)
  507. {
  508.     FSClose(gOutputRefNum);
  509. }
  510.